package org.lsda.job;

import org.lsda.caller.LsdaSender;
import org.lsda.util.Util;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class is used to represent the job for work.
 * */
public class LsdaJob implements Job {

	private static Log log = LogFactory.getLog(LsdaJob.class);

	private String groupId = "";

	private String jobId = "";

	private int sched_seq = -1;

	private String description = "";

	private String updateTime = "";

	private String triggerName = "";

	private String triggerGroup = "";

	private ArrayList<LsdaAction> actions = new ArrayList<LsdaAction>();

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public ArrayList<LsdaAction> getActions() {
		return actions;
	}

	public void setActions(ArrayList<LsdaAction> actions) {
		this.actions = actions;
	}

	public int getSched_seq() {
		return sched_seq;
	}

	public void setSched_seq(int sched_seq) {
		this.sched_seq = sched_seq;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public String getTriggerName() {
		return triggerName;
	}

	public void setTriggerName(String triggerName) {
		this.triggerName = triggerName;
	}

	public String getTriggerGroup() {
		return triggerGroup;
	}

	public void setTriggerGroup(String triggerGroup) {
		this.triggerGroup = triggerGroup;
	}

	public boolean buildJob(Connection conn) throws SQLException {
		boolean ret = false;
		try {
			if (sched_seq == -1) {
				sched_seq = Util.autoIncrementID("LSDA_SCHED_SEQ");
				// insert job
				Util.insertJob(conn, groupId, jobId, sched_seq, triggerGroup,
						triggerName);

				// insert the 0 step for building action
				Util.insertAction(conn, sched_seq, 0, "building_action", null);
				Util.markActionStart(conn, sched_seq, 0);

				// insert all the actions
				for (int i = 0; i < actions.size(); i++) {
					LsdaAction action = actions.get(i);
					Util.insertAction(conn, sched_seq, i + 1,
							action.getPrgName(), action.getPrgParam());
				}

				Util.markActionEnd(conn, sched_seq, 0,
						LsdaAction.STATUS_SUCCESS, "build ok");
			} else {
				int aseq = Util.getActionStatus(conn, sched_seq, 0);
				// build instruction failed
				if (LsdaAction.STATUS_SUCCESS != aseq) {
					Util.clearAction(conn, sched_seq);
					// insert the 0 step for building action
					Util.insertAction(conn, sched_seq, 0, "building_action",
							null);
					Util.markActionStart(conn, sched_seq, 0);

					// insert all the actions
					for (int i = 0; i < actions.size(); i++) {
						LsdaAction action = actions.get(i);
						Util.insertAction(conn, sched_seq, i + 1,
								action.getPrgName(), action.getPrgParam());
					}

					Util.markActionEnd(conn, sched_seq, 0,
							LsdaAction.STATUS_SUCCESS, "build ok");
				} else {
					//
					Util.markJobReady(conn, sched_seq);
				}
			}
			ret = true;
		} catch (Exception ex) {
			ret = false;
			log.error(ex.getMessage());
			Util.markActionEnd(conn, sched_seq, 0, LsdaAction.STATUS_FAILED,
					ex.getLocalizedMessage());
		}

		return ret;
	}

	public boolean doAction(LsdaAction act) {
		boolean ret = false;
		try {
			LsdaSender sender = new LsdaSender();
			sender.send(act);
			ret = true;
		} catch (Exception ex) {
			ret = false;
			log.error(ex.getMessage());
		}

		return ret;
	}

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		boolean ret = false;
		Connection conn = null;
		try {
			Trigger trigger = arg0.getTrigger();
			triggerGroup = trigger.getKey().getGroup();
			triggerName = trigger.getKey().getName();
			conn = Util.getConnection();
			ret = buildJob(conn);
			// fail to build job
			if (ret == false)
				return;
			int act_seq = 0;
			LsdaAction action = Util.getNextActionToRun(conn, sched_seq,act_seq);
			
			while (action != null && doAction(action)){
				act_seq = action.getActionSeq();
				log.info("act seq:" + act_seq);
				action = Util.getNextActionToRun(conn, sched_seq,act_seq);
			}

		} catch (SQLException ex) {
			log.error(ex.getMessage());

		} catch (ClassNotFoundException e) {
			log.error(e.getMessage());
		}
	}

	public static void main(String args[]) {

	}
}
